home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 676-700 / 681 / term / source.lha / Scale.c < prev    next >
C/C++ Source or Header  |  1992-05-09  |  8KB  |  427 lines

  1. /*
  2. **    $Id: Scale.c,v 1.3 92/05/01 12:52:56 olsen Sta Locker: olsen $
  3. **    $Revision: 1.3 $
  4. **    $Date: 92/05/01 12:52:56 $
  5. **
  6. **    Single scaled character output routines
  7. **
  8. **    Copyright © 1990-1992 by Olaf `Olsen' Barthel & MXM
  9. **        All Rights Reserved
  10. */
  11.  
  12. #include "termGlobal.h"
  13.  
  14.     /* Some static data required by the bitmap scaling routine. */
  15.  
  16. STATIC struct RastPort        *ScaleRPort;
  17. STATIC struct BitMap        *ScaleSrcBitMap,
  18.                 *ScaleDstBitMap;
  19. STATIC struct BitScaleArgs    *ScaleArgs;
  20.  
  21. STATIC WORD             ScaleCache = -1;
  22.  
  23.     /* DeleteScale():
  24.      *
  25.      *    Frees all the data associated with font scaling.
  26.      */
  27.  
  28. VOID
  29. DeleteScale()
  30. {
  31.     if(ScaleArgs)
  32.     {
  33.         FreeVec(ScaleArgs);
  34.  
  35.         ScaleArgs = NULL;
  36.     }
  37.  
  38.     if(ScaleDstBitMap)
  39.     {
  40.         if(ScaleDstBitMap -> Planes[0])
  41.             FreeVec(ScaleDstBitMap -> Planes[0]);
  42.  
  43.         FreeVec(ScaleDstBitMap);
  44.  
  45.         ScaleDstBitMap = NULL;
  46.     }
  47.  
  48.     if(ScaleSrcBitMap)
  49.     {
  50.         if(ScaleSrcBitMap -> Planes[0])
  51.             FreeVec(ScaleSrcBitMap -> Planes[0]);
  52.  
  53.         FreeVec(ScaleSrcBitMap);
  54.  
  55.         ScaleSrcBitMap = NULL;
  56.     }
  57.  
  58.     if(ScaleRPort)
  59.     {
  60.         FreeVec(ScaleRPort);
  61.  
  62.         ScaleRPort = NULL;
  63.     }
  64. }
  65.  
  66.     /* CreateScale():
  67.      *
  68.      *    Sets up the data required for real-time font scaling
  69.      *    (bitmaps, rastports, etc.).
  70.      */
  71.  
  72. BYTE
  73. CreateScale()
  74. {
  75.     WORD i;
  76.  
  77.         /* Create a RastPort to render into. */
  78.  
  79.     if(ScaleRPort = (struct RastPort *)AllocVec(sizeof(struct RastPort),MEMF_ANY|MEMF_CLEAR))
  80.     {
  81.             /* Initialize it. */
  82.  
  83.         InitRastPort(ScaleRPort);
  84.  
  85.             /* Create the bitmap to render into. */
  86.  
  87.         if(ScaleSrcBitMap = (struct BitMap *)AllocVec(sizeof(struct BitMap),MEMF_ANY|MEMF_CLEAR))
  88.         {
  89.                 /* Initialize the bitmap. */
  90.  
  91.             InitBitMap(ScaleSrcBitMap,Screen -> RastPort . BitMap -> Depth,8,8);
  92.  
  93.                 /* Create the bitmap to place the scaled font data into. */
  94.  
  95.             if(ScaleDstBitMap = (struct BitMap *)AllocVec(sizeof(struct BitMap),MEMF_ANY|MEMF_CLEAR))
  96.             {
  97.                     /* Initialize it, must be able to hold four
  98.                      * times the size of the source data.
  99.                      */
  100.  
  101.                 InitBitMap(ScaleDstBitMap,Screen -> RastPort . BitMap -> Depth,16,16);
  102.  
  103.                     /* Put the source bitmap into the source RastPort. */
  104.  
  105.                 ScaleRPort -> BitMap = ScaleSrcBitMap;
  106.  
  107.                     /* Allocate the necessary memory space. */
  108.  
  109.                 if(ScaleSrcBitMap -> Planes[0] = (PLANEPTR)AllocVec(ScaleSrcBitMap -> BytesPerRow * ScaleSrcBitMap -> Rows * ScaleSrcBitMap -> Depth,MEMF_CHIP|MEMF_CLEAR))
  110.                 {
  111.                         /* Fill in the planes. */
  112.  
  113.                     for(i = 1 ; i < ScaleSrcBitMap -> Depth ; i++)
  114.                         ScaleSrcBitMap -> Planes[i] = ScaleSrcBitMap -> Planes[i - 1] + ScaleSrcBitMap -> BytesPerRow * ScaleSrcBitMap -> Rows;
  115.  
  116.                         /* Allocate space for the destination area. */
  117.  
  118.                     if(ScaleDstBitMap -> Planes[0] = (PLANEPTR)AllocVec(ScaleDstBitMap -> BytesPerRow * ScaleDstBitMap -> Rows * ScaleDstBitMap -> Depth,MEMF_CHIP|MEMF_CLEAR))
  119.                     {
  120.                             /* Fill in the remaining planes. */
  121.  
  122.                         for(i = 1 ; i < ScaleDstBitMap -> Depth ; i++)
  123.                             ScaleDstBitMap -> Planes[i] = ScaleDstBitMap -> Planes[i - 1] + ScaleDstBitMap -> BytesPerRow * ScaleDstBitMap -> Rows;
  124.  
  125.                             /* Install the fonts. */
  126.  
  127.                         SetFont(ScaleRPort,CurrentFont);
  128.  
  129.                             /* Set the default rendering pens. */
  130.  
  131.                         SetAPen(ScaleRPort,1);
  132.                         SetBPen(ScaleRPort,0);
  133.  
  134.                             /* By default, overwrite data. */
  135.  
  136.                         SetDrMd(ScaleRPort,JAM2);
  137.  
  138.                             /* Allocate space for the bitmap scaling arguments. */
  139.  
  140.                         if(ScaleArgs = (struct BitScaleArgs *)AllocVec(sizeof(struct BitScaleArgs),MEMF_ANY|MEMF_CLEAR))
  141.                         {
  142.                                 /* Initialize the structure. */
  143.  
  144.                             ScaleArgs -> bsa_SrcWidth    = 8;
  145.                             ScaleArgs -> bsa_SrcHeight    = 8;
  146.  
  147.                             ScaleArgs -> bsa_YSrcFactor    = 1;
  148.  
  149.                             ScaleArgs -> bsa_SrcBitMap    = ScaleSrcBitMap;
  150.                             ScaleArgs -> bsa_DestBitMap    = ScaleDstBitMap;
  151.  
  152.                             return(TRUE);
  153.                         }
  154.                     }
  155.                 }
  156.             }
  157.         }
  158.     }
  159.  
  160.     return(FALSE);
  161. }
  162.  
  163.     /* PrintScaled(UBYTE Char,UBYTE Scale):
  164.      *
  165.      *    This is the big one: since VT100 supports a number of
  166.      *    font sizes (double height, double width, 132 columns),
  167.      *    the approriate characters are scaled in real-time before
  168.      *    they are displayed.
  169.      */
  170.  
  171. VOID __regargs
  172. PrintScaled(UBYTE *Buffer,LONG Size,UBYTE Scale)
  173. {
  174.     UWORD SrcY,DestX,DestY,SizeX,Baseline;
  175.  
  176.         /* Determine the scale of the destination character. */
  177.  
  178.     if(Config . FontScale == SCALE_HALF)
  179.     {
  180.             /* Determine scale to be used. */
  181.  
  182.         switch(Scale)
  183.         {
  184.                 /* Half width. */
  185.  
  186.             case SCALE_ATTR_NORMAL:
  187.  
  188.                 ScaleArgs -> bsa_XDestFactor    = 1;
  189.                 ScaleArgs -> bsa_YDestFactor    = 1;
  190.                 ScaleArgs -> bsa_XSrcFactor    = 2;
  191.  
  192.                 SrcY    = 0;
  193.                 DestX    = CursorX * 4;
  194.                 SizeX    = 4;
  195.  
  196.                 ScaleCache = -1;
  197.  
  198.                 break;
  199.  
  200.                 /* Half width, double height (top bits). */
  201.  
  202.             case SCALE_ATTR_TOP2X:
  203.  
  204.                 ScaleArgs -> bsa_XDestFactor    = 1;
  205.                 ScaleArgs -> bsa_YDestFactor    = 2;
  206.                 ScaleArgs -> bsa_XSrcFactor    = 1;
  207.  
  208.                 SrcY    = 0;
  209.                 DestX    = CursorX * 8;
  210.                 SizeX    = 8;
  211.  
  212.                 ScaleCache = -1;
  213.  
  214.                 break;
  215.  
  216.                 /* Half width, double height (bottom bits). */
  217.  
  218.             case SCALE_ATTR_BOT2X:
  219.  
  220.                 ScaleArgs -> bsa_XDestFactor    = 1;
  221.                 ScaleArgs -> bsa_YDestFactor    = 2;
  222.                 ScaleArgs -> bsa_XSrcFactor    = 1;
  223.  
  224.                 SrcY    = 8;
  225.                 DestX    = CursorX * 8;
  226.                 SizeX    = 8;
  227.  
  228.                 ScaleCache = -1;
  229.  
  230.                 break;
  231.         }
  232.     }
  233.     else
  234.     {
  235.             /* Determine scale to be used. */
  236.  
  237.         switch(Scale)
  238.         {
  239.                 /* Double height (top bits). */
  240.  
  241.             case SCALE_ATTR_TOP2X:
  242.  
  243.                 ScaleArgs -> bsa_XDestFactor    = 2;
  244.                 ScaleArgs -> bsa_YDestFactor    = 2;
  245.                 ScaleArgs -> bsa_XSrcFactor    = 1;
  246.  
  247.                 SrcY    = 0;
  248.                 DestX    = CursorX * 16;
  249.                 SizeX    = 16;
  250.  
  251.                 ScaleCache = -1;
  252.  
  253.                 break;
  254.  
  255.                 /* Double height (bottom bits). */
  256.  
  257.             case SCALE_ATTR_BOT2X:
  258.  
  259.                 ScaleArgs -> bsa_XDestFactor    = 2;
  260.                 ScaleArgs -> bsa_YDestFactor    = 2;
  261.                 ScaleArgs -> bsa_XSrcFactor    = 1;
  262.  
  263.                 SrcY    = 8;
  264.                 DestX    = CursorX * 16;
  265.                 SizeX    = 16;
  266.  
  267.                 ScaleCache = -1;
  268.  
  269.                 break;
  270.  
  271.                 /* Double width. */
  272.  
  273.             case SCALE_ATTR_2X:
  274.  
  275.                 ScaleArgs -> bsa_XDestFactor    = 2;
  276.                 ScaleArgs -> bsa_YDestFactor    = 1;
  277.                 ScaleArgs -> bsa_XSrcFactor    = 1;
  278.  
  279.                 SrcY    = 0;
  280.                 DestX    = CursorX * 16;
  281.                 SizeX    = 16;
  282.  
  283.                 ScaleCache = -1;
  284.  
  285.                 break;
  286.         }
  287.     }
  288.  
  289.         /* Look for the font type to scale. */
  290.  
  291.     if(ScaleRPort -> Font != CurrentFont)
  292.     {
  293.         SetFont(ScaleRPort,CurrentFont);
  294.  
  295.         ScaleCache = -1;
  296.     }
  297.  
  298.         /* Set the approriate colours. */
  299.  
  300.     if(ScaleRPort -> FgPen != RPort -> FgPen)
  301.     {
  302.         SetAPen(ScaleRPort,RPort -> FgPen);
  303.  
  304.         ScaleCache = -1;
  305.     }
  306.  
  307.     if(ScaleRPort -> BgPen != RPort -> BgPen)
  308.     {
  309.         SetBPen(ScaleRPort,RPort -> BgPen);
  310.  
  311.         ScaleCache = -1;
  312.     }
  313.  
  314.         /* Calculate topmost line to write to. */
  315.  
  316.     DestY = CursorY * 8;
  317.  
  318.         /* Remember the font baseline. */
  319.  
  320.     Baseline = CurrentFont -> tf_Baseline;
  321.  
  322.     if(CurrentFont == GFX)
  323.     {
  324.         BYTE Mode = 1;
  325.  
  326.             /* Run down the buffer... */
  327.  
  328.         while(Size--)
  329.         {
  330.             if(GfxTable[*Buffer] == Mode)
  331.             {
  332.                 if(*Buffer != ScaleCache)
  333.                 {
  334.                     ScaleCache = *Buffer;
  335.  
  336.                         /* Print the character to be scaled into the
  337.                          * invisible drawing area.
  338.                          */
  339.  
  340.                     Move(ScaleRPort,0,Baseline);
  341.  
  342.                     Text(ScaleRPort,Buffer++,1);
  343.  
  344.                         /* Scale the font. */
  345.  
  346.                     BitMapScale(ScaleArgs);
  347.                 }
  348.                 else
  349.                     Buffer++;
  350.  
  351.                     /* Render the character. */
  352.  
  353.                 BltBitMapRastPort(ScaleDstBitMap,0,SrcY,RPort,DestX,DestY,SizeX,8,0xC0);
  354.             }
  355.             else
  356.             {
  357.                 ScaleCache = *Buffer;
  358.  
  359.                 if(Mode)
  360.                 {
  361.                     if(Config . Font == FONT_IBM && IBM)
  362.                         SetFont(ScaleRPort,IBM);
  363.                     else
  364.                         SetFont(ScaleRPort,Topaz);
  365.                 }
  366.                 else
  367.                     SetFont(ScaleRPort,GFX);
  368.  
  369.                 Mode ^= 1;
  370.  
  371.                     /* Print the character to be scaled into the
  372.                      * invisible drawing area.
  373.                      */
  374.  
  375.                 Move(ScaleRPort,0,Baseline);
  376.  
  377.                 Text(ScaleRPort,Buffer++,1);
  378.  
  379.                     /* Scale the font. */
  380.  
  381.                 BitMapScale(ScaleArgs);
  382.  
  383.                     /* Render the character. */
  384.  
  385.                 BltBitMapRastPort(ScaleDstBitMap,0,SrcY,RPort,DestX,DestY,SizeX,8,0xC0);
  386.             }
  387.  
  388.             DestX += SizeX;
  389.         }
  390.  
  391.         if(!Mode)
  392.             SetFont(ScaleRPort,GFX);
  393.     }
  394.     else
  395.     {
  396.             /* Run down the buffer... */
  397.  
  398.         while(Size--)
  399.         {
  400.             if(*Buffer != ScaleCache)
  401.             {
  402.                 ScaleCache = *Buffer;
  403.  
  404.                     /* Print the character to be scaled into the
  405.                      * invisible drawing area.
  406.                      */
  407.  
  408.                 Move(ScaleRPort,0,Baseline);
  409.  
  410.                 Text(ScaleRPort,Buffer++,1);
  411.  
  412.                     /* Scale the font. */
  413.  
  414.                 BitMapScale(ScaleArgs);
  415.             }
  416.             else
  417.                 Buffer++;
  418.  
  419.                 /* Render the character. */
  420.  
  421.             BltBitMapRastPort(ScaleDstBitMap,0,SrcY,RPort,DestX,DestY,SizeX,8,0xC0);
  422.  
  423.             DestX += SizeX;
  424.         }
  425.     }
  426. }
  427.